package com.zllkj.mm.adapter

import android.view.LayoutInflater
import android.widget.CheckBox
import androidx.appcompat.widget.*
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zllkj.mm.R
import com.zllkj.mm.common.ClientConfig
import com.zllkj.mm.model.entity.OrderListItem
import com.zllkj.mm.module.shopping.vm.ItemShoppingCartIListVM
import java.math.BigDecimal

class ShoppingCartListAdapter(layoutResId: Int, data: List<ItemShoppingCartIListVM?>?) :
        BaseQuickAdapter<ItemShoppingCartIListVM, BaseViewHolder>(layoutResId, data) {

    public var onShoppingCartCheckListener : OnShoppingCartCheckListener?=null

    interface OnShoppingCartCheckListener{
        fun onAllCheck(isCheck:Boolean)
        fun onSubGoodsCheck(isCheck: Boolean,subPosition:Int)
        fun onGoodsCountUpdate(position:Int,subPosition: Int,count:Int)
    }

    override fun convert(baseViewHolder: BaseViewHolder, vm: ItemShoppingCartIListVM) {
        baseViewHolder.getView<AppCompatTextView>(R.id.tv_shopping_cart_item_shopname).text = vm.shopName
        baseViewHolder.addOnClickListener(R.id.ll_each_one_goods_shopping_cart_area)


        var goodsArea = baseViewHolder.getView<LinearLayoutCompat>(R.id.ll_each_one_goods_shopping_cart_area)
        goodsArea.removeAllViews()
        var radiobuttonShop = baseViewHolder.getView<AppCompatCheckBox>(R.id.rb_shop_checkbox)
        radiobuttonShop.isChecked = vm.shopCheckBoxIsChecked


        radiobuttonShop.setOnCheckedChangeListener {
            buttonView, isChecked ->
            vm.shopCheckBoxIsChecked = isChecked
            for(i in 0 until goodsArea.childCount){
                var childRb = goodsArea.getChildAt(i).findViewById<AppCompatCheckBox>(R.id.rb_each_one_goods_check)
                vm.goodsCheckedFlag[i] = isChecked
                childRb.isChecked = isChecked
            }
            onShoppingCartCheckListener?.let {
                it.onAllCheck(isChecked)
            }

        }
        for ( i in 0 until vm.goodsCheckedFlag.size){
            vm.goodsCheckedFlag[i] = vm.shopCheckBoxIsChecked
        }


        vm.goodsList.mapIndexed { index , it ->
            var view = LayoutInflater.from(mContext).inflate(R.layout.item_oach_one_shopping_cart,null)

            Glide.with(mContext).load(ClientConfig.IMG_URL + it.goodsPic).into(view.findViewById<AppCompatImageView>(R.id.iv_each_one_goods_image))
            view.findViewById<AppCompatTextView>(R.id.tv_each_one_goods_name).text = it.goodsName
            view.findViewById<AppCompatTextView>(R.id.tv_each_one_goods_count).text = it.goodsNum.toString()
            view.findViewById<AppCompatTextView>(R.id.tv_each_one_goods_unitPrice).text = "¥" + BigDecimal(it.unitPrice).divide(BigDecimal("100")).toEngineeringString()
            view.findViewById<AppCompatTextView>(R.id.tv_shopping_cart_add_count).setOnClickListener {  addBtn->
                vm.goodsList[index].goodsNum = vm.goodsList[index].goodsNum +1
                if (onShoppingCartCheckListener != null) {
                    onShoppingCartCheckListener?.onGoodsCountUpdate(data.indexOf(vm),index,vm.goodsList[index].goodsNum)
                }
            }
            view.findViewById<AppCompatTextView>(R.id.tv_shopping_cart_reduce).setOnClickListener {  reduceBtn ->
                if(vm.goodsList[index].goodsNum > 1) {
                    vm.goodsList[index].goodsNum = vm.goodsList[index].goodsNum - 1
                    onShoppingCartCheckListener?.onGoodsCountUpdate(data.indexOf(vm),index,vm.goodsList[index].goodsNum)
                }
            }
            var checkBox =  view.findViewById<CheckBox>(R.id.rb_each_one_goods_check);
            checkBox.isChecked = vm.goodsCheckedFlag[index]
            checkBox.setOnCheckedChangeListener { buttonView, isChecked ->

                vm.goodsCheckedFlag[index] = isChecked

                onShoppingCartCheckListener?.let {
                    it.onSubGoodsCheck(isChecked,index)
                }
                var checkCount = 0

                vm.goodsCheckedFlag.forEachIndexed { index, b ->
                    if (b) checkCount += 1
                }
                if (checkCount == vm.goodsList.size) {
                    vm.shopCheckBoxIsChecked = true
                    radiobuttonShop.isChecked = true
                } else {
                    vm.shopCheckBoxIsChecked = false
                    radiobuttonShop.isChecked = false
                }
            }


            goodsArea.addView(view)
        }

    }


}